package com.example.forecast.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.forecast.R;
import com.example.forecast.bean.Lunar;
import com.example.forecast.bean.Solar;
import com.example.forecast.util.LiuRenUtil;
import com.example.forecast.util.LunarSolarConverter;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CharacterActivity extends AppCompatActivity {

    private DatePicker datePicker;
    private TimePicker timePicker;
    private TextView solarDate, character, wood, fire, earth, metal, water;
    private LiuRenUtil liuRenUtil;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_character);

        datePicker = findViewById(R.id.dp_character_date);
        timePicker = findViewById(R.id.tp_character_time);
        solarDate = findViewById(R.id.tv_character_solar);
        character = findViewById(R.id.tv_character_lunar);

        wood = findViewById(R.id.tv_character_wood);
        fire = findViewById(R.id.tv_character_fire);
        earth = findViewById(R.id.tv_character_earth);
        metal = findViewById(R.id.tv_character_metal);
        water = findViewById(R.id.tv_character_water);

        deduceCharacter(0);

        datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                deduceCharacter(1);
            }
        });

        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                deduceCharacter(1);
            }
        });
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/23 15:47
     * @param: int state(状态值) 0为当前时间起八字 , 1为输入时间起八字
     * @effect:
     * @return:
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void deduceCharacter(int state) {

        int elements[];

        if (state == 0) {
            Date date = new Date();
            Solar solar = new Solar();//阳历对象
            solar.solarYear = Integer.valueOf(new SimpleDateFormat("yyyy").format(date));//获取阳历年份
            solar.solarMonth = Integer.valueOf(new SimpleDateFormat("MM").format(date));//获取阳历月份
            solar.solarDay = Integer.valueOf(new SimpleDateFormat("dd").format(date));//获取阳历日期

            Lunar lunar = LunarSolarConverter.SolarToLunar(solar);//转换为阴历对象

            int hour = Integer.valueOf(new SimpleDateFormat("HH").format(date));
            liuRenUtil = new LiuRenUtil(solar, lunar, date);
            solarDate.setText("您的出生年月日时为：" + solar.solarYear + "年" + solar.solarMonth + "月" + solar.solarDay + "日" + hour + "时");

        } else {
            Solar solar = new Solar();//阳历对象
            solar.solarYear = datePicker.getYear();//获取阳历年份
            solar.solarMonth = datePicker.getMonth() + 1;//获取阳历月份
            solar.solarDay = datePicker.getDayOfMonth();//获取阳历日期

            Lunar lunar = LunarSolarConverter.SolarToLunar(solar);//转换为阴历对象

            int hour = timePicker.getHour();
            int minute = timePicker.getMinute();

            liuRenUtil = new LiuRenUtil(solar, lunar, hour, minute);

            solarDate.setText("您的出生年月日时为：" + solar.solarYear + "年" + solar.solarMonth + "月" + solar.solarDay + "日" + hour + "时");
        }

        character.setText("您的八字为：" + liuRenUtil.getCharacter(1));

        elements = liuRenUtil.getElements();
        wood.setText("木：" + elements[0]);
        fire.setText("火：" + elements[1]);
        earth.setText("土：" + elements[2]);
        metal.setText("金：" + elements[3]);
        water.setText("水：" + elements[4]);

    }
}